<?php

class Product extends MY_Controller {
    
    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        
        $this->load->model("brand_home_model");
        $this->load->model("category_home_model");
       // $this->load->model("slider_home_model");
        $this->load->model("product_home_model");
        
    }   

    public function brand($id)
    {
        
        $data['brand_name'] = $this->brand_home_model->get_info('bra_id', $id);
        $start = $this->uri->segment(3);
        if(!$start) $start = 0; 
        $data['list_pro'] = $this->product_home_model->brand_pro($id, $start, 4);
        $config['base_url'] = base_url()."/".$id."/brand.html"; // xác định trang phân trang 
        $config['total_rows'] = count($this->product_home_model->brand_pro($id)); // xác định tổng số record 
        $config['per_page'] = 4; // xác định số record ở mỗi trang 
        $config['uri_segment'] = 3; // xác định segment chứa page number 
        $this->pagination->initialize($config);
        $data['page_link'] = $this->pagination->create_links();
        $this->layout_home('product/index',$data);
    }
    public function category($id)
    {
       
        $data['cate_name'] = $this->category_home_model->get_info('cate_id', $id);
        $start = $this->uri->segment(3);
        if(!$start) $start = 0;
        $data['list_pro'] = $this->product_home_model->cate_pro($id, $start, 6);
        $config['base_url'] = base_url()."".$id."/category.html";
        $config['total_rows'] = count($this->product_home_model->cate_pro($id));
        $config['per_page'] = 6;
        $config['uri_segment'] = 3;
        $this->pagination->initialize($config);
        $data['page_link'] = $this->pagination->create_links();
        
       $this->layout_home('product/index',$data);
    }
    public function sortcate()
    {
        $val = $this->input->post('sortcate');
        $id = $this->input->post('idcate');
        $data = $this->product_home_model->cate_pro($id,0,'',$val);
       
        $html = $this->product_helper->show_product($data);
        echo $html;
        
    }
    public function sortbrand()
    {
        $val = $this->input->post('sortbrand');
        $id = $this->input->post('idbrand');
        $data = $this->product_home_model->brand_pro($id,0,'',$val);
        $html = $this->product_helper->show_product($data);
        echo $html;
        
    }
    public function range_price()
    {
        $start = $this->input->post('start');
        $end = $this->input->post('end');
       
        $data = $this->product_home_model->list_range_price($start, $end);
        $html = $this->product_helper->show_product($data);
        echo $html;
    }
    public function details($id_pro = '')
    {
        $data['product'] = $this->product_home_model->get_info($id_pro);
        $data['imgs'] = $this->product_home_model->get_imgs($id_pro);
        $avg = $this->product_home_model->get_rating_avg($id_pro);
        $data['rating_avg'] = $avg[0]['avg_rate'];
        $data['list_comment'] = $this->product_home_model->list_comment($id_pro);
        $this->layout_home('product/details', $data);
    }
    public function star($pro_id = '')
    {
        $aResponse['error'] = false;
        $aResponse['message'] = '';
        if(isset($_POST['action']))
        {
                if(htmlentities($_POST['action'], ENT_QUOTES, 'UTF-8') == 'rating')
                {
                        $rate = floatval($_POST['rate']);
                        $this->product_home_model->insert_rating($pro_id, $rate);
                        // YOUR MYSQL REQUEST HERE or other thing :)
                        /*
                        *
                        */

                        // if request successful
                        $success = true;
                        // else $success = false;
                        // json datas send to the js file
                        if($success)
                        {
                                $aResponse['message'] = 'Your rate has been successfuly recorded. Thanks for your rate :)';
                                echo json_encode($aResponse);
                        }
                        else
                        {
                                $aResponse['error'] = true;
                                $aResponse['message'] = 'An error occured during the request. Please retry';
                                
                        }
                }
                else
                {
                        $aResponse['error'] = true;
                        $aResponse['message'] = '"action" post data not equal to \'rating\'';
                        
                }
        }
        else
        {
                $aResponse['error'] = true;
                $aResponse['message'] = '$_POST[\'action\'] not found';
                
        }
    }
    public function comment($pro_id = '')
    {
        
        $cus_name = $this->input->post('cus_name');
        $comment = $this->input->post('comment');
        $this->product_home_model->insert_comment($pro_id,$cus_name, $comment );
        $data = array(
            "cus_name" => $cus_name,
            "comment" => $comment
        );
        $html = $this->product_helper->show_comment($data);
        echo $html;
        
    }
}

